package main.leetcode.competition.lcp.spring;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class lcp2020 {
    private int res;
    private HashMap<Integer, List<Integer>> hashMap;
    private int minRes;

    public static void main(String[] args) {
        //        System.out.println(new test1().minCount(new int[] {4, 2, 1}));
        //        System.out.println(new test1().minCount(new int[] {2, 3, 10}));
        //        System.out.println("[[2,8,4],[2,5,0],[10,9,8]]".replace("[", "{").replace("]",
        // "}"));
        //        System.out.println(
        //                "[[2,11,3],[15,10,7],[9,17,12],[8,1,14]]".replace("[", "{").replace("]",
        // "}"));
        System.out.println(
                Arrays.toString(
                        new lcp2020()
                                .getTriggerTime(
                                        new int[][] {{0, 4, 5}, {4, 8, 8}, {8, 6, 1}, {10, 10, 0}},
                                        new int[][] {
                                            {12, 11, 16},
                                            {20, 2, 6},
                                            {9, 2, 6},
                                            {10, 18, 3},
                                            {8, 14, 9}
                                        })));
        System.out.println(
                Arrays.toString(
                        new lcp2020()
                                .getTriggerTime(
                                        new int[][] {{2, 8, 4}, {2, 5, 0}, {10, 9, 8}},
                                        new int[][] {
                                            {2, 11, 3}, {15, 10, 7}, {9, 17, 12}, {8, 1, 14}
                                        })));
        System.out.println(
                Arrays.toString(
                        new lcp2020()
                                .getTriggerTime(new int[][] {{1, 1, 1}}, new int[][] {{0, 0, 0}})));

        //        System.out.println(
        //                "[[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]]".replace("[",
        // "{").replace("]", "}"));
        //        System.out.println(
        //                new test1()
        //                        .numWays(
        //                                5,
        //                                new int[][] {
        //                                    {0, 2}, {2, 1}, {3, 4}, {2, 3}, {1, 4}, {2, 0}, {0, 4}
        //                                },
        //                                3));
        //        System.out.println(new test1().numWays(3, new int[][] {{0, 2}, {2, 1}}, 2));

        //        System.out.println(new lcp2020().minJump(new int[] {2, 5, 1, 1, 1, 1}));
        //        System.out.println(new lcp2020().minJump(new int[] {2, 5, 3, 4, 2, 1}));
        //        System.out.println(new lcp2020().minJump(new int[] {2, 2, 2, 22, 5, 3, 1, 2}));
        //        System.out.println(new lcp2020().minJump(new int[] {1, 2, 1, 3, 1, 2, 4, 5}));
        //        System.out.println(
        //                new lcp2020().minJump(new int[] {8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7,
        // 8}));
        //        System.out.println(new lcp2020().minJump(new int[] {1}));
        System.out.println(
                new lcp2020()
                        .minJump(
                                new int[] {
                                    3, 7, 6, 1, 4, 3, 7, 8, 1, 2, 8, 5, 9, 8, 3, 2, 7, 5, 1, 1
                                }));
        System.out.println(
                new lcp2020()
                        .minJump(
                                new int[] {
                                    4, 6, 10, 8, 3, 5, 3, 5, 7, 8, 6, 10, 3, 7, 3, 10, 7, 10, 10, 9,
                                    1, 4, 7, 4, 8, 6, 9, 8, 8, 2, 7, 2, 4, 5, 4, 3, 3, 2, 2, 2, 3,
                                    4, 4, 1, 1, 5, 6, 8, 1, 2
                                }));
    }

    public int minCount(int[] coins) {
        int res = 0;
        for (int coin : coins) {
            res += (coin & 1) == 1 ? coin / 2 + 1 : coin / 2;
        }
        return res;
    }

    public int numWays(int n, int[][] relation, int k) {
        res = 0;
        hashMap = new HashMap<>(n);
        List<Integer> list;
        for (int[] ints : relation) {
            list = hashMap.get(ints[1]);
            if (list == null) {
                list = new LinkedList<>();
            }
            list.add(ints[0]);
            hashMap.put(ints[1], list);
        }
        numWays(n - 1, 0, k);
        return res;
    }

    private boolean numWays(int target, int cur, int k) {
        if (cur == k && target == 0) return true;
        else if (cur == k) return false;
        List<Integer> list = hashMap.get(target);
        if (list == null) return false;
        for (int i : hashMap.get(target)) {
            if (numWays(i, cur + 1, k)) ++res;
        }
        return false;
    }

    public int[] getTriggerTime(int[][] increase, int[][] requirements) {
        int day = 1;
        int n = increase.length;
        int m = requirements.length;
        int[] cur = new int[3];
        int[] res = new int[m];
        Arrays.fill(res, -1);
        if (n == 0) return res;
        // 第0天，对比触发条件
        for (int i = 0; i < n; ++i, ++day) {
            for (int j = 0; j < 3; ++j) {
                cur[j] += increase[i][j];
            }
            // 对比触发条件
            for (int k = 0; k < m; ++k) {
                // 已经完成
                if (res[k] >= 0) continue;
                for (int j = 0; j < 3; ++j) {
                    if (cur[j] < requirements[k][j]) {
                        break;
                    }
                    if (j == 2) {
                        res[k] = day;
                    }
                }
            }
        }
        return res;
    }

    public int minJump(int[] jump) {
        minRes = Integer.MAX_VALUE;
        minCount(jump, jump[0], 1);
        return minRes;
    }

    private void minCount(int[] jump, int curIndex, int res) {
        if (res > minRes || curIndex < 1) return;
        if (curIndex > jump.length - 1) {
            minRes = res;
            return;
        }
        minCount(jump, curIndex + jump[curIndex], res + 1);
        for (int i = 1; i < curIndex; ++i) {
            minCount(jump, i, res + 1);
        }
    }
}
